package com.cdf.iapp.netty;

import java.util.concurrent.TimeUnit;

import android.util.Log;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class Client {
	public static void main(String[] args) throws Exception {
		Client client = new Client();
		Thread.sleep(1000 * 5);
		client.sendData("123");
	}

	String remoteAddr = "47.97.103.128";
	int remotePort = 18003;
	private Channel channel;
	private EventLoopGroup loop = new NioEventLoopGroup();

	public void sendData(String content) throws Exception {
		if (channel != null && channel.isActive()) {
			ByteBuf delimiter = Unpooled.copiedBuffer((content + "_@")
					.getBytes());
			channel.writeAndFlush(delimiter);
		}

	}

	public void create(Bootstrap bootstrap, EventLoopGroup workGroup) {
		try {
			bootstrap.group(workGroup).channel(NioSocketChannel.class)
					.handler(new ChannelInitializer<SocketChannel>() {
						protected void initChannel(SocketChannel socketChannel)
								throws Exception {
							ChannelPipeline p = socketChannel.pipeline();
							p.addLast(new MyChannelInitializer());
						}
					});
			ChannelFuture future = bootstrap.connect(remoteAddr, remotePort);
			future.addListener(new ChannelFutureListener() {
				public void operationComplete(ChannelFuture futureListener)
						throws Exception {
					if (futureListener.isSuccess()) {
						channel = futureListener.channel();
						Log.i("log", "Connect to server successfully!");
					} else {
						Log.i("log",
								"Failed to connect to server, try connect after 10s");
						final EventLoop workGroup = futureListener.channel()
								.eventLoop();
						futureListener.channel().eventLoop()
								.schedule(new Runnable() {
									@Override
									public void run() {
										create(new Bootstrap(), workGroup);
									}
								}, 10, TimeUnit.SECONDS);
					}
				}
			});

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void run() {
		create(new Bootstrap(), loop);
	}

	// protected void doConnect() {
	// if (channel != null && channel.isActive()) {
	// return;
	// }
	//
	// ChannelFuture future = bootstrap.connect(remoteAddr, remotePort);
	//
	// future.addListener(new ChannelFutureListener() {
	// public void operationComplete(ChannelFuture futureListener) throws
	// Exception {
	// if (futureListener.isSuccess()) {
	// channel = futureListener.channel();
	// Log.i("log","Connect to server successfully!");
	// } else {
	// Log.i("log","Failed to connect to server, try connect after 10s");
	//
	// futureListener.channel().eventLoop().schedule(new Runnable() {
	// @Override
	// public void run() {
	// doConnect();
	// }
	// }, 10, TimeUnit.SECONDS);
	// }
	// }
	// });
	// }

}